package publishsubscribe.shangguigu;

import cn.hutool.core.io.file.FileWriter;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import utils.RabbitMQUtil;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

public class MyReceiveLogs02 {

    // 交换机名称
    private static final String EXCHANGE_NAME = "logs";

    public static void main(String[] args) throws IOException, TimeoutException {
        // 获取信道
        Channel channel = RabbitMQUtil.getChannel();
        // 获取临时队列名称
        String queueName = channel.queueDeclare().getQueue();
        // 交换机和队列绑定
        channel.queueBind(queueName, EXCHANGE_NAME, "");
        System.out.println(" [*] 把日志存储到文件中...");
        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
            FileWriter fileWriter = new FileWriter("C:\\Users\\hz21076971\\Desktop\\myJava\\rabbitmq_info.txt");
            fileWriter.write(message,true);
            System.out.println("数据写入文件成功");
        };
        channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {});
    }
}
